<template>
  <div class="membershipCardManagement">
    <h3>会员卡管理</h3>
    <div class="search">
      <div class="cond1 cond">
        <el-input placeholder="请输入标题" v-model="search1">
          <template slot="prepend">标题</template>
        </el-input>
      </div>
      <div>
        <el-button type="primary" icon="el-icon-search" @click="searchMethod">搜索</el-button>
      </div>
    </div>
    <div class="opration">
      <el-button
        type="success"
        icon="el-icon-plus"
        @click="addData"
        v-if="permissions_obj.id_14||permissions_obj.id_all"
      >新增</el-button>
    </div>
    <div class="list">
      <el-table
        border
        ref="multipleTable"
        :data="tableData"
        tooltip-effect="dark"
        style="width: 100%"
      >
        <el-table-column type="index" width="50" header-align="center" align="center" label="序号"></el-table-column>
        <el-table-column header-align="center" align="center" prop="title" label="标题" width="120"></el-table-column>
        <el-table-column header-align="center" align="center" prop="day" label="天数" width="90"></el-table-column>
        <el-table-column
          header-align="center"
          align="center"
          prop="send_type_text"
          label="赠送类型"
          width="90"
        ></el-table-column>
        <el-table-column
          header-align="center"
          align="center"
          prop="send_num"
          label="赠送数量"
          width="90"
        ></el-table-column>
        <el-table-column
          header-align="center"
          align="center"
          prop="true_price"
          label="价格"
          width="90"
        ></el-table-column>
        <el-table-column header-align="center" align="center" prop="remark" label="标题描述"></el-table-column>
        <el-table-column header-align="center" align="center" prop="intro" label="价格描述"></el-table-column>
        <el-table-column
          header-align="center"
          align="center"
          prop="status_text"
          label="状态"
          width="80"
        ></el-table-column>
        <el-table-column header-align="center" align="center" prop="sort" label="排序" width="80"></el-table-column>
        <el-table-column
          header-align="center"
          align="center"
          prop="created_at"
          label="创建时间"
          width="180"
        ></el-table-column>
        <el-table-column
          header-align="center"
          align="center"
          prop="updated_at"
          label="编辑时间"
          width="180"
        ></el-table-column>
        <el-table-column header-align="center" align="center" label="操作" width="100" fixed="right">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="warning"
              @click="handleEdit(scope.$index, scope.row)"
              v-if="permissions_obj.id_15||permissions_obj.id_all"
            >编辑</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="page">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page.sync="currentPage1"
        :page-sizes="[10,30,50,100]"
        :page-size="pageSize"
        layout="total, prev, pager, next,sizes,jumper"
        :total="totalSize"
      ></el-pagination>
    </div>
    <!-- //新增、修改弹窗 -->
    <el-dialog :title="dialogTitle" :visible.sync="modifyEditorDialogVisible" width="40%" center>
      <div class="row">
        <span>标题：</span>
        <el-input placeholder="请输入VIP标题" v-model="inputVIPTitle" clearable></el-input>
      </div>
      <div class="row">
        <span>天数：</span>
        <el-input placeholder="请输入天数" v-model="inputDayNum" clearable type="number"></el-input>
      </div>
      <div class="row">
        <span>赠送类型：</span>
        <el-select v-model="inputGiveAwayType" placeholder="请选择">
          <el-option
            v-for="item in optionsGiveAwayType"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
      </div>
      <div class="row" v-if="inputGiveAwayType!=0">
        <span>赠送数量：</span>
        <el-input placeholder="请输入赠送数量" v-model="inputGiveAwayNum" clearable></el-input>
      </div>
      <div class="row">
        <span>价格：</span>
        <el-input placeholder="请输入价格" v-model="inputCurrentPrice" clearable></el-input>
      </div>
      <div class="row">
        <span>标题描述：</span>
        <el-input placeholder="请输入描述信息" v-model="inputTitleDescription" clearable></el-input>
      </div>
      <div class="row">
        <span>价格描述：</span>
        <el-input placeholder="请输入描述信息" v-model="inputDescription" clearable></el-input>
      </div>
      <div class="row">
        <span>排序：</span>
        <el-input placeholder="请输入排序" v-model="inputSort" clearable type="number"></el-input>
      </div>
      <div class="row">
        <span>状态：</span>
        <el-select v-model="inputStatus" placeholder="请选择">
          <el-option
            v-for="item in options2"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
      </div>
      <div class="dialogBtn">
        <el-button type="primary" size="small" @click="submitModifyEditor">提交</el-button>
        <el-button type="info" size="small" @click="cancelModifyEditor">取消</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
export default {
  name: "membershipCardManagement",
  data() {
    return {
      search1: "",
      tableData: [],
      currentPage1: 1,
      pageSize: 10,
      totalSize: 0,
      dialogTitle: "修改vip信息",
      modifyEditorDialogVisible: false,
      options2: [
        {
          value: 0,
          label: "未启动"
        },
        {
          value: 1,
          label: "启动"
        }
      ],
      optionsGiveAwayType: [
        {
          value: 0,
          label: "不赠送"
        },
        {
          value: 1,
          label: "会员"
        },
        {
          value: 2,
          label: "钻石"
        }
      ],
      //弹窗会员卡标题
      inputVIPTitle: "",
      //弹窗天数
      inputDayNum: "",
      //弹窗赠送类型
      inputGiveAwayType: "",
      //弹窗赠送数量
      inputGiveAwayNum: 0,
      //弹窗价格
      inputCurrentPrice: "",
      //弹窗标题描述信息
      inputTitleDescription: "",
      //弹窗价格描述信息
      inputDescription: "",
      //弹窗排序
      inputSort: "",
      //弹窗状态
      inputStatus: "",
      initData: {},
      selectId: "",
      // 权限管理
      permissions: [],
      permissions_obj: {}
    };
  },
  created() {
    //权限判断
    this.permissions = this.$store.state.permissions;
    let oneOff = false;
    this.permissions.forEach((item, index) => {
      if (item == -1) {
        oneOff = true;
      } else {
        this.permissions_obj["id_" + item] = item;
      }
    });
    if (oneOff) {
      this.permissions_obj.id_all = true;
    } else {
      this.permissions_obj.id_all = false;
    }
  },
  mounted() {
    this.getDataList();
  },
  methods: {
    // 分页
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.currentPage1 = val;
      this.getDataList();
    },
    handleSizeChange(val) {
      this.pageSize = val;
      this.getDataList();
    },
    //新增
    addData() {
      this.inputVIPTitle = "";
      this.inputDayNum = "";
      this.inputGiveAwayType = "";
      this.inputGiveAwayNum = 0;
      this.inputCurrentPrice = "";
      this.inputStatus = "";
      this.inputTitleDescription = "";
      this.inputDescription = "";
      this.inputSort = "";
      this.modifyEditorDialogVisible = true;
      this.dialogTitle = "新建vip信息";
    },
    //编辑
    handleEdit(index, row) {
      this.inputVIPTitle = "";
      this.inputDayNum = "";
      this.inputGiveAwayType = "";
      this.inputGiveAwayNum = 0;
      this.inputCurrentPrice = "";
      this.inputStatus = "";
      this.inputTitleDescription = "";
      this.inputDescription = "";
      this.inputSort = "";
      this.modifyEditorDialogVisible = true;
      this.dialogTitle = "修改vip信息";
      this.selectId = row.id;
      this.getEditorDetail();
    },
    //提交修改或者新增
    submitModifyEditor() {
      if (this.dialogTitle == "新建vip信息") {
        this.submitAddMethod();
      } else if (this.dialogTitle == "修改vip信息") {
        this.submitModifyMethod();
      }
    },
    //取消提交
    cancelModifyEditor() {
      this.modifyEditorDialogVisible = false;
    },
    searchMethod() {
      this.currentPage1 = 1;
      this.getDataList();
    },
    //获取初始化会员卡列表
    async getDataList() {
      let parames = {
        handshake: "",
        data: {
          page: this.currentPage1,
          page_size: this.pageSize,
          search: {
            title: this.search1
          }
        }
      };
      let res = await this.$axios.post(
        this.$api.cardList,
        JSON.stringify(parames)
      );
      if (res.code == 200) {
        this.initData = res.data;
        this.initData.data.forEach((item, index) => {
          if (item.status == 0) {
            item.status_text = "未启用";
          } else if (item.status == 1) {
            item.status_text = "启用";
          }
          if (item.send_type == 1) {
            item.send_type_text = "会员";
          } else if (item.send_type == 2) {
            item.send_type_text = "钻石";
          }else if (item.send_type == 0) {
            item.send_type_text = "不赠送";
          }
        });
        this.totalSize = this.initData.total;
        this.tableData = this.initData.data;
      } else {
        this.$message({
          message: res.message,
          type: "warning"
        });
      }
    },
    //提交新增接口
    async submitAddMethod() {
      if (!this.inputVIPTitle) {
        this.$message({
          message: "请填写标题",
          type: "warning"
        });
        return;
      }
      if (!this.inputDayNum) {
        this.$message({
          message: "请填写会员天数",
          type: "warning"
        });
        return;
      }
      let parames = {
        handshake: "",
        data: {
          title: this.inputVIPTitle,
          day: this.inputDayNum,
          send_type: this.inputGiveAwayType,
          send_num: this.inputGiveAwayNum,
          true_price: this.inputCurrentPrice,
          remark: this.inputTitleDescription,
          status: this.inputStatus,
          intro: this.inputDescription,
          sort: this.inputSort,
          send_remark: ""
        }
      };
      let res = await this.$axios.post(
        this.$api.cardAdd,
        JSON.stringify(parames)
      );
      if (res.code == 200) {
        this.$message({
          message: res.message,
          type: "success"
        });
        this.modifyEditorDialogVisible = false;
        this.getDataList();
      } else {
        this.$message({
          message: res.message,
          type: "warning"
        });
      }
    },
    //获取编辑详情
    async getEditorDetail() {
      let parames = {
        handshake: "",
        data: {
          id: this.selectId
        }
      };
      let res = await this.$axios.post(
        this.$api.cardDetail,
        JSON.stringify(parames)
      );
      if (res.code == 200) {
        let data = res.data;
        this.inputVIPTitle = data.title;
        this.inputDayNum = data.day;
        this.inputGiveAwayType=data.send_type;
        this.inputGiveAwayNum=data.send_num;
        this.inputCurrentPrice = data.true_price;
        this.inputTitleDescription=data.remark;
        this.inputStatus = data.status;
        this.inputDescription = data.intro;
        this.inputSort = data.sort;
      } else {
        this.$message({
          message: res.message,
          type: "warning"
        });
      }
    },
    //提交修改接口
    async submitModifyMethod() {
      if (!this.inputVIPTitle) {
        this.$message({
          message: "请填写标题",
          type: "warning"
        });
        return;
      }
      if (!this.inputDayNum) {
        this.$message({
          message: "请填写会员天数",
          type: "warning"
        });
        return;
      }
      let parames = {
        handshake: "",
        data: {
          id: this.selectId,
          title: this.inputVIPTitle,
          day: this.inputDayNum,
          send_type: this.inputGiveAwayType,
          send_num: this.inputGiveAwayNum,
          true_price: this.inputCurrentPrice,
          remark: this.inputTitleDescription,
          status: this.inputStatus,
          intro: this.inputDescription,
          sort: this.inputSort,
          send_remark: ""
        }
      };
      let res = await this.$axios.post(
        this.$api.cardUpdate,
        JSON.stringify(parames)
      );
      if (res.code == 200) {
        this.$message({
          message: res.message,
          type: "success"
        });
        this.modifyEditorDialogVisible = false;
        this.getDataList();
      } else {
        this.$message({
          message: res.message,
          type: "warning"
        });
      }
    }
  }
};
</script>
<style scoped>
h3 {
  padding: 0.2rem;
  color: #3c763d;
}
.search {
  display: flex;
  margin-top: 0.2rem;
  padding-left: 0.2rem;
  width: 100%;
  flex-wrap: wrap;
}
.el-input {
  width: 2.5rem;
}
label {
  width: 0.83rem;
  height: 0.4rem;
  display: flex;
  justify-content: center;
  align-items: center;
  background: #f5f7fa;
  color: #909399;
  border: 1px solid #dcdfe6;
  border-top-left-radius: 4px;
  border-bottom-left-radius: 4px;
  font-size: 14px;
}
.cond {
  display: flex;
  margin-right: 0.4rem;
  margin-bottom: 0.1rem;
}
.search .el-select >>> .el-input__inner {
  border-left: none;
  border-top-left-radius: 0px;
  border-bottom-left-radius: 0px;
}
.search .el-select {
  width: 1.67rem;
}
.opration {
  margin-top: 0.2rem;
  padding-left: 0.2rem;
}
.page {
  margin-top: 0.2rem;
  display: flex;
  justify-content: center;
}
.list {
  margin-top: 0.2rem;
  padding: 0 0.1rem;
  font-size: 0.12rem;
}
.dialogBtn {
  display: flex;
  justify-content: center;
  margin-top: 0.3rem;
}
.row {
  margin-bottom: 0.1rem;
  display: flex;
  align-items: center;
}
.row span {
  width: 1rem;
  display: block;
}
</style>